package com.autumn.mybatis;

import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.alibaba.fastjson.util.IOUtils;
import com.autumn.mybatis.mapper.MapperRegister;
import com.autumn.mybatis.provider.mysql.MySqlProvider;
import com.autumn.mybatis.session.AutumnSqlSessionFactoryBuilder;

/**
 * Db 测试
 * 
 * @author 杨昌国
 *
 *         2017-10-17 14:57:18
 */
public class DbTestUtils {

	private static SqlSessionFactory sqlSessionFactory;
	static {
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader("mybatis.xml");
			sqlSessionFactory = new AutumnSqlSessionFactoryBuilder().build(reader);
			MapperRegister regMap = new MapperRegister(new MySqlProvider());
			regMap.configure(sqlSessionFactory.getConfiguration());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.close(reader);
		}
	}

	/**
	 * 获取Session
	 * 
	 * @return
	 */
	public static SqlSession getSqlSession() {
		return sqlSessionFactory.openSession(true);
	}
}
